'use strict';
const crypto = require('crypto');
const jwt = require('jwt-simple')
const { tokenExp } = require('const-data');
const encryptPassword = require('encrypt-password');
const db = uniCloud.database()
exports.main = async (event, context) => {
  let {
  	email,
  	password,
  } = event
  
  let userInfo = {
  	email
  }
  
  let userInDB = await db.collection('users').where(userInfo).get();
  
  let tokenSecret = crypto.randomBytes(16).toString('hex');  //用来生成token
  let token = jwt.encode(userInfo,tokenSecret)
  
  let userUpdateResult;
  
  if(userInDB.data && userInDB.data.length === 0){  //如果不存在该用户
  	userUpdateResult = await db.collection('users').add({
  		...userInfo,
  		password:encryptPassword(password),
		relevanceUserID:-1,  // 关联小程序
  		tokenSecret,
  		exp:Date.now() + tokenExp
  	})
  } else {
  	return {
  		status:-1,
  		msg:"此用户已经存在"
  	}
  }
  
  if(userUpdateResult.id || userUpdateResult.affectedDocs === 1){
  	return{
  		status:0,
  		token,
  		msg:"注册成功"
  	}
  }
  
  return {
    status: -1,
    msg: '注册失败'
  }
};
